# ~*~ coding: utf-8 ~*~

from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired


from apps.common.form import ErrorFormMinx
from apps.common.validators import Number


class PermissionCreateUpdateForm(ErrorFormMinx, FlaskForm):
    name = StringField(validators=[InputRequired('权限名称不能为空')])
    pid = StringField()
    module = StringField()
    controller = StringField()
    action = StringField()
    is_menu = StringField()
    icon = StringField()
    sort = StringField(validators=[Number('排序必须为数字')])

    @property
    def data(self):
        # return dict((name, f.data) for name, f in iteritems(self._fields))
        return {
            'name': self.name.data.strip(),
            'pid': self.pid.data.strip(),
            'module': self.module.data.strip(),
            'controller': self.controller.data.strip(),
            'action': self.action.data.strip(),
            'is_menu': int(self.is_menu.data) if self.is_menu.data else 0,
            'icon': self.icon.data.strip(),
            'sort': int(self.sort.data) if self.sort.data else 0,
        }


class RoleCreateUpdateForm(ErrorFormMinx, FlaskForm):
    name = StringField(validators=[InputRequired('权限名称不能为空')])
    describe = StringField()
    permissions = StringField()

    @property
    def data(self):
        # return dict((name, f.data) for name, f in iteritems(self._fields))
        return {
            'name': self.name.data,
            'describe': self.describe.data,
            'permissions': self.permissions.data,
        }
